﻿using DongFangGuoKai.WMSPlatform.WebApi.Collection;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;
using System.Net.WebSockets;
using System.Text;

namespace DongFangGuoKai.WMSPlatform.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class WebSocketController : ControllerBase
    {



        /// <summary>
        /// 
        /// </summary>
        /// <param name="action">逻辑处理</param>
        /// <param name="subChannels">频道集合</param>
        [NonAction]

        private void ReidsSubTopic(Action<ChannelMessage> action, List<string> subChannels)
        {
            // 创建Redis连接
            var connection = "10.10.20.180:9736,password=Hik@1234";
            var redis = ConnectionMultiplexer.Connect(connection);
            Console.WriteLine($"Redis连接状态：{redis.IsConnected}");
            //获取订阅者实例
            var sub = redis.GetSubscriber();


            //订阅CTU
            //sub.Subscribe(new RedisChannel(subChannelCTU, RedisChannel.PatternMode.Pattern), (chanel, message) =>
            //{
            //    string msg = (string)message;
            //    Console.WriteLine(msg);
            //});
            ////订阅叉车
            //sub.Subscribe(subChannelCar, (chanel, message) =>
            //{
            //    string msg = (string)message;
            //    Console.WriteLine(msg);
            //});  
            if (subChannels != null && subChannels.Count > 0)
            {
                foreach (var channel in subChannels)
                {

                    Console.WriteLine($"-------{channel}开始订阅-------------------------");
                    //订阅话题
                    //var channelMesaageQuece = sub.Subscribe(channel);
                    //channelMesaageQuece.OnMessage(action);

                    //        sub.Subscribe(channel, (chanel, message) =>
                    //{
                    //    string msg = (string)message;
                    //    Console.WriteLine(msg);
                    //});

                    //订阅CTU
                    sub.Subscribe(new RedisChannel(channel, RedisChannel.PatternMode.Auto), (chanel, message) =>
                    {
                        string msg = (string)message;
                        Console.WriteLine(msg);
                    });
                }

            }

        }

        [HttpGet]
        [Route("/Sub")]
        public async Task Sub()
        {
            List<string> subChannels = new List<string>()
            {
                 "RCS_ROBOT_123ABCD01AB01AB_XY",
                 "RCS_ROBOT_123ABCD01AB01AB_AA"
            };

            //伪代码逻辑
            Action<ChannelMessage> action = m =>
            {

                Console.WriteLine("-------------订阅收到消息开始-------------------");

                Console.WriteLine(m.Message);

                Console.WriteLine("-------------订阅收到消息结束-------------------");
            };
            ReidsSubTopic(action, subChannels);




        }

    }
}
